import java.util.Arrays;

public class TimeTravellingGogo {
	public long determineTime(int n, int[] sunnyStart, int[] sunnyEnd, String[] roads, int machineStartTime) {
		int[][] graph = new int[n][n];
		StringBuilder roadsDescription = new StringBuilder();
		for (String line : roads)
			roadsDescription.append(line);
		String[] roadDescriptions = roadsDescription.toString().split(" ");
		for (String roadDescription : roadDescriptions) {
			String[] tokenizedRoadDescription = roadDescription.split(",");
			int from = Integer.parseInt(tokenizedRoadDescription[0]);
			int to = Integer.parseInt(tokenizedRoadDescription[1]);
			int cost = Integer.parseInt(tokenizedRoadDescription[2]);
			graph[from][to] = graph[to][from] = cost;
		}
		long[][] distance = new long[n][n];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (graph[i][j] != 0)
					distance[i][j] = graph[i][j];
				else if (i != j)
					distance[i][j] = Long.MAX_VALUE / 2;
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				for (int k = 0; k < n; k++)
					distance[j][k] = Math.min(distance[j][i] + distance[i][k], distance[j][k]);
			}
		}
		int[] intervalStart = new int[sunnyStart.length + 1];
		int[] intervalEnd = new int[sunnyStart.length + 1];
		for (int i = 0; i < sunnyStart.length; i++) {
			intervalStart[i + 1] = sunnyStart[i];
			intervalEnd[i + 1] = sunnyEnd[i];
		}
		int m = intervalStart.length;
		long[][][] noTimeTravel = new long[n][m][n];
		for (int i = 0; i < n; i++) {
			for (int j = m - 1; j >= 0; j--) {
				Arrays.fill(noTimeTravel[i][j], Long.MAX_VALUE / 2);
				noTimeTravel[i][j][i] = intervalEnd[j];
				if (j == m - 1)
					continue;
				for (int k = 0; k < n; k++) {
					if (distance[i][k] <= intervalEnd[j + 1] - intervalStart[j + 1]) {
						noTimeTravel[i][j][k] = intervalStart[j + 1] + distance[i][k];
						for (int l = 0; l < n; l++)
							noTimeTravel[i][j][l] = Math.min(noTimeTravel[i][j][l], noTimeTravel[k][j + 1][l]);
					}
				}
			}
		}
		long[][] atIntervalEnd = new long[n][m];
		for (int i = 1; i < n; i++)
			Arrays.fill(atIntervalEnd[i], Long.MAX_VALUE / 2);
		for (int i = 0; i < m; i++)
			atIntervalEnd[0][i] = intervalEnd[i];
/*		while (true) {
			boolean update = false;
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					for (int k = 0; k < n; k++) {
						for (int l = 0; l <= j; l++) {
							if (distance[])
							long current = atIntervalEnd[i][j] + machineStartTime
						}
					}
				}
			}
		}*/
		return -1;
	}


// BEGIN CUT HERE
	public static void main(String[] args) {
		if (args.length == 0) {
			TimeTravellingGogoHarness.run_test(-1);
		} else {
			for (int i=0; i<args.length; ++i)
				TimeTravellingGogoHarness.run_test(Integer.valueOf(args[i]));
		}
	}
// END CUT HERE
}

// BEGIN CUT HERE
class TimeTravellingGogoHarness {
	public static void run_test(int casenum) {
		if (casenum != -1) {
			if (runTestCase(casenum) == -1)
				System.err.println("Illegal input! Test case " + casenum + " does not exist.");
			return;
		}
		
		int correct = 0, total = 0;
		for (int i=0;; ++i) {
			int x = runTestCase(i);
			if (x == -1) {
				if (i >= 100) break;
				continue;
			}
			correct += x;
			++total;
		}
		
		if (total == 0) {
			System.err.println("No test cases run.");
		} else if (correct < total) {
			System.err.println("Some cases FAILED (passed " + correct + " of " + total + ").");
		} else {
			System.err.println("All " + total + " tests passed!");
		}
	}
	
	static boolean compareOutput(long expected, long result) { return expected == result; }
	static String formatResult(long res) {
		return String.format("%d", res);
	}
	
	static int verifyCase(int casenum, long expected, long received) { 
		System.err.print("Example " + casenum + "... ");
		if (compareOutput(expected, received)) {
			System.err.println("PASSED");
			return 1;
		} else {
			System.err.println("FAILED");
			System.err.println("    Expected: " + formatResult(expected)); 
			System.err.println("    Received: " + formatResult(received)); 
			return 0;
		}
	}

	static int runTestCase(int casenum) {
		switch(casenum) {
		case 0: {
			int N                     = 3;
			int[] sunnyStart          = {0,8};
			int[] sunnyEnd            = {4,12};
			String[] roads            = {"0,1,3 1,2,3"};
			int machineStartTime      = 1;
			long expected__           = 9L;

			return verifyCase(casenum, expected__, new TimeTravellingGogo().determineTime(N, sunnyStart, sunnyEnd, roads, machineStartTime));
		}
		case 1: {
			int N                     = 3;
			int[] sunnyStart          = {0,8};
			int[] sunnyEnd            = {4,12};
			String[] roads            = {"0,1,3 1,2,4"};
			int machineStartTime      = 18;
			long expected__           = 12L;

			return verifyCase(casenum, expected__, new TimeTravellingGogo().determineTime(N, sunnyStart, sunnyEnd, roads, machineStartTime));
		}
		case 2: {
			int N                     = 2;
			int[] sunnyStart          = {1,3};
			int[] sunnyEnd            = {2,4};
			String[] roads            = {"0,1,2"};
			int machineStartTime      = 1;
			long expected__           = -1L;

			return verifyCase(casenum, expected__, new TimeTravellingGogo().determineTime(N, sunnyStart, sunnyEnd, roads, machineStartTime));
		}
		case 3: {
			int N                     = 3;
			int[] sunnyStart          = {0,17};
			int[] sunnyEnd            = {15,37};
			String[] roads            = {"0,1,1","5"," 1,2,12 2,0,17"};
			int machineStartTime      = 10;
			long expected__           = 29L;

			return verifyCase(casenum, expected__, new TimeTravellingGogo().determineTime(N, sunnyStart, sunnyEnd, roads, machineStartTime));
		}

		// custom cases

/*      case 4: {
			int N                     = ;
			int[] sunnyStart          = ;
			int[] sunnyEnd            = ;
			String[] roads            = ;
			int machineStartTime      = ;
			long expected__           = L;

			return verifyCase(casenum, expected__, new TimeTravellingGogo().determineTime(N, sunnyStart, sunnyEnd, roads, machineStartTime));
		}*/
/*      case 5: {
			int N                     = ;
			int[] sunnyStart          = ;
			int[] sunnyEnd            = ;
			String[] roads            = ;
			int machineStartTime      = ;
			long expected__           = L;

			return verifyCase(casenum, expected__, new TimeTravellingGogo().determineTime(N, sunnyStart, sunnyEnd, roads, machineStartTime));
		}*/
/*      case 6: {
			int N                     = ;
			int[] sunnyStart          = ;
			int[] sunnyEnd            = ;
			String[] roads            = ;
			int machineStartTime      = ;
			long expected__           = L;

			return verifyCase(casenum, expected__, new TimeTravellingGogo().determineTime(N, sunnyStart, sunnyEnd, roads, machineStartTime));
		}*/
		default:
			return -1;
		}
	}
}

// END CUT HERE
